<?php
class ControllerPhoneNumber extends Controller {
    private $error = array();

    public function report() {
    	$this->language->load('phone/number');
    	
        $this->document->setTitle(sprintf($this->language->get('report_page_title'), $number, $number));
    	$this->document->setDescription($this->config->get('config_meta_description'));
    	$this->document->setKeywords($product_info['meta_keyword']);
        
        if (isset($this->request->post['captcha'])) {
            $this->data['captcha'] = $this->request->post['captcha'];
        } else {
            $this->data['captcha'] = '';
        }
        if (isset($this->error['captcha'])) {
            $this->data['error_captcha'] = $this->error['captcha'];
        } else {
            $this->data['error_captcha'] = '';
        }

        if (isset($this->request->post['number'])) {
            $this->data['number'] = $this->request->post['number'];
        } else {
            $this->data['number'] = '';
        }

        if (isset($this->request->post['comment'])) {
            $this->data['comment'] = $this->request->post['comment'];
        } else {
            $this->data['comment'] = '';
        }
        if (isset($this->request->post['owner'])) {
        	$this->data['owner'] = $this->request->post['owner'];
        } else {
        	$this->data['owner'] = '';
        }
        if (isset($this->request->post['user'])) {
        	$this->data['user'] = $this->request->post['user'];
        } else {
        	$this->data['user'] = '';
        }
        $this->data['error_number'] = '';
        $this->load->model('phone/call_type');
        
        $this->data['call_types'] = $this->model_phone_call_type->getCallTypes();
        $this->data['captcha_link'] = $this->url->link('phone/number/captcha', '', 'SSL');
        $this->data['title'] = $this->language->get('title_report_phone_number');
        $this->data['message'] = $this->language->get('report_message');
        
        $this->data['text_report_number'] = $this->language->get('text_report_number');
        $this->data['text_phone_number'] = $this->language->get('text_phone_number');
        $this->data['text_comment'] = $this->language->get('text_comment');
        $this->data['text_who_was_it'] = $this->language->get('text_who_was_it');
        $this->data['text_you_name'] = $this->language->get('text_you_name');
        $this->data['text_captcha'] = $this->language->get('text_captcha');
        $this->data['text_call_type'] = $this->language->get('text_call_type');
        $this->data['text_hide'] = $this->language->get('text_hide');
        
        $this->data['text_note_phone_number'] = $this->language->get('text_note_phone_number');
        $this->data['text_note_comment'] = $this->language->get('text_note_comment');
        $this->data['text_note_who_was_it'] = $this->language->get('text_note_who_was_it');
        $this->data['text_note_you_name'] = $this->language->get('text_note_you_name');
        $this->data['text_note_captcha'] = $this->language->get('text_note_captcha');
        
        $this->data['button_close'] = $this->language->get('button_close');
        $this->data['button_send_report'] = $this->language->get('button_send_report');
        
        $this->children = array(
            'common/content_top',
            'common/content_bottom',
            'common/footer',
            'common/header'
        );
        
        $this->template = 'ukphone/template/phone/report.tpl';

        $this->response->setOutput($this->render());
    }

    public function search() {

		$this->language->load('phone/number');    	
    	$this->load->model('phone/number');
    	$this->load->model('phone/comment');
    	$this->load->model('phone/call_type');
    	$number = $this->request->get['num'];
    	
    	$this->document->setTitle(sprintf($this->language->get('search_page_title'), $number, $number));
    	$this->document->setDescription($this->config->get('config_meta_description'));
    	$this->document->setKeywords($product_info['meta_keyword']);
    
    	$phone = $this->model_phone_number->getNumberByText($number);
    
    	$this->model_phone_number->addCount($phone['phone_id']);
    	
    	$page = 1;
    	if (isset($this->request->get['page']) && is_numeric($this->request->get['page'])) {
    		$page = (int)$this->request->get['page'];
    	}
    	
    	$comment_total = $phone['total_comments'];
    	$pagination = new PaginationNew();
    	$pagination->total = $comment_total;
    	$pagination->page = $page;
    	$pagination->limit = $this->config->get('phone_config_comment_limit');
    	$pagination->text = $this->language->get('text_pagination');
    	$pagination->url = $this->url->link('phone/number/search', 'num='.$number.'&page={page}', 'SSL');
    	$start = ($page - 1) * $pagination->limit;
    	
    	$data = array(
    			'phone_id' => $phone['phone_id'],
    			'start' => $start,
    			'limit' => $pagination->limit
    	);
    	
    	$comments = array();
    	if(!empty($phone['phone_id']))
    		$comments = $this->model_phone_comment->getComments($data);
    	
    	$this->data['call_types'] = $this->model_phone_call_type->getCallTypes();
    	$this->data['captcha_link'] = $this->url->link('phone/number/captcha', '', 'SSL');   
    	$this->data['pagination'] = $pagination->render();
    	$this->data['phone'] = $phone;
    	$comment_no = ($page - 1) * $pagination->limit + 1;
    	
    	foreach($comments as $comment){
    		$this->data['comments'][] = array(
    				'phone_comment_id' => $comment['phone_comment_id'],
    				'nickname' => $comment['nickname'],
    				'comment' => $comment['comment'],
    				'created' => $comment['created'],
    				'header' => sprintf($this->language->get('comment_header'), $comment['nickname'], $comment['created']),
    				'footer' => sprintf($this->language->get('comment_footer'), $comment['rate'], $comment_no++, $phone['total_comments'], $this->getValueInArray($this->data['call_types'], $comment['call_type_id']))
    		);
    	}
    	
    	if (isset($this->request->post['captcha'])) {
    		$this->data['captcha'] = $this->request->post['captcha'];
    	} else {
    		$this->data['captcha'] = '';
    	}
    	if (isset($this->error['captcha'])) {
    		$this->data['error_captcha'] = $this->error['captcha'];
    	} else {
    		$this->data['error_captcha'] = '';
    	}
    	
    	if (isset($this->request->post['number'])) {
    		$this->data['number'] = $this->request->post['number'];
    	} else {
    		$this->data['number'] = '';
    	}
    	
    	if (isset($this->request->post['comment'])) {
    		$this->data['comment'] = $this->request->post['comment'];
    	} else {
    		$this->data['comment'] = '';
    	}
    	if (isset($this->request->post['owner'])) {
    		$this->data['owner'] = $this->request->post['owner'];
    	} else {
    		$this->data['owner'] = '';
    	}
    	if (isset($this->request->post['user'])) {
    		$this->data['user'] = $this->request->post['user'];
    	} else {
    		$this->data['user'] = '';
    	}
    	
    	$this->data['text_report_number'] = $this->language->get('text_report_number');
    	$this->data['text_phone_number'] = $this->language->get('text_phone_number');
    	$this->data['text_comment'] = $this->language->get('text_comment');
    	$this->data['text_who_was_it'] = $this->language->get('text_who_was_it');
    	$this->data['text_you_name'] = $this->language->get('text_you_name');
    	$this->data['text_captcha'] = $this->language->get('text_captcha');
    	$this->data['text_call_type'] = $this->language->get('text_call_type');
    	$this->data['text_hide'] = $this->language->get('text_hide');
    	
    	$this->data['text_note_phone_number'] = $this->language->get('text_note_phone_number');
    	$this->data['text_note_comment'] = $this->language->get('text_note_comment');
    	$this->data['text_note_who_was_it'] = $this->language->get('text_note_who_was_it');
    	$this->data['text_note_you_name'] = $this->language->get('text_note_you_name');
    	$this->data['text_note_captcha'] = $this->language->get('text_note_captcha');
    	$this->data['title_before_comments'] = $this->language->get('title_before_comments');
    	$this->data['message_before_comments'] = sprintf($this->language->get('message_before_comments'), $number);
    	
    	$this->data['button_close'] = $this->language->get('button_close');
    	$this->data['button_send_report'] = $this->language->get('button_send_report');
    	
    	$this->template = 'ukphone/template/phone/search.tpl';
    	$this->children = array(
    			'common/content_top',
    			'common/content_bottom',
    			'common/footer',
    			'common/header'
    	);
    
    	$this->response->setOutput($this->render());
    }
    
    public function top_search() {
    	$this->language->load('phone/number');
    	$this->load->model('phone/number');
    	
    	$this->document->setTitle(sprintf($this->language->get('top_search_page_title'), $number, $number));
    	$this->document->setDescription($this->config->get('config_meta_description'));
    	$this->document->setKeywords($product_info['meta_keyword']);
    
    	$number_total = $this->model_phone_number->getTotalNumbers();
    	$page = 1;
    	if(isset($this->request->get['page']) && is_numeric($this->request->get['page']))
    		$page = intval($this->request->get['page']);
    	
    	$pagination = new PaginationNew();
    	$pagination->total = $number_total;
    	$pagination->page = $page;
    	$pagination->limit = $this->config->get('phone_config_top_search_limit');
    	$pagination->text = $this->language->get('text_pagination');
    	$pagination->url = $this->url->link('phone/number/top_search', 'page={page}', 'SSL');
    	$start = ($page - 1) * $pagination->limit;

        $this->data['text_lastest_search'] = $this->language->get('text_lastest_search');
        $this->data['title_search'] = $this->language->get('title_search');
        
    	$data = array(
    			'start' => $start,
    			'limit' => $pagination->limit,
    			'page' => $page,
    			'sort' => 'p.total_search',
    			'order' => 'DESC'
    	);
    	$phones = $this->model_phone_number->getNumbers($data);
    	
    	foreach($phones as $phone){
    		$this->data['phones'][] = array(
    				'number' => $phone['number'],
    				'href' => $this->url->link('phone/number/search', 'num='.$phone['number'], 'SSL')
    		);
    	}

    	$this->data['pagination'] = $pagination->render();
    
    	$this->template = 'ukphone/template/phone/top_search.tpl';
    	$this->children = array(
    			'common/content_top',
    			'common/content_bottom',
    			'common/footer',
    			'common/header'
    	);
    
    	$this->response->setOutput($this->render());
    }
    
    public function process() {
        $this->language->load('phone/number');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
        	$this->load->model('phone/number');
        	$phone = $this->model_phone_number->addNumber($this->request->post);
        	$this->data['number'] = $phone;
        }
        
        if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
        	$server = $this->config->get('config_ssl');
        } else {
        	$server = $this->config->get('config_url');
        }

        $this->document->setTitle($this->language->get('process_page_title'));
        $this->document->setDescription($this->config->get('config_meta_description'));
        $this->document->setKeywords($product_info['meta_keyword']);
        $this->data['heading_title'] = $this->config->get('config_title');

        if (isset($this->request->post['captcha'])) {
            $this->data['captcha'] = $this->request->post['captcha'];
        } else {
            $this->data['captcha'] = '';
        }

        if (isset($this->error['captcha'])) {
            $this->data['error_captcha'] = $this->error['captcha'];
        } else {
            $this->data['error_captcha'] = '';
        }

        if (isset($this->error['number'])) {
            $this->data['error_number'] = $this->error['number'];
        } else {
            $this->data['error_number'] = '';
        }

        if (isset($this->request->post['comment'])) {
            $this->data['comment'] = $this->request->post['comment'];
        } else {
            $this->data['comment'] = '';
        }

        if (isset($this->request->post['owner'])) {
            $this->data['owner'] = $this->request->post['owner'];
        } else {
            $this->data['owner'] = '';
        }

        if (isset($this->request->post['user'])) {
            $this->data['user'] = $this->request->post['user'];
        } else {
            $this->data['user'] = '';
        }

        $this->load->model('phone/call_type');

        $this->data['call_types'] = $this->model_phone_call_type->getCallTypes();
        $this->data['msg'] = $this->language->get('report_message');
        $this->data['title_warning'] = $this->language->get('title_warning');
        $this->data['warning_message'] = $this->language->get('warning_message');
        if(!empty($phone)){
	        $this->data['title_vote_success'] = sprintf($this->language->get('title_vote_success'), $this->url->link('phone/number/search' , 'num=' . $phone['number'], 'SSL'), $phone['number']);
	        $this->data['text_back'] = sprintf($this->language->get('text_back'), $phone['number']);
	        $this->data['back_link'] = $this->url->link('phone/number/search' , 'num=' . $phone['number'], 'SSL');
	        
	        $this->data['text_home'] = $this->language->get('text_home');
	        $this->data['home_link'] = $server;
	        
	        $this->data['text_report'] = $this->language->get('text_report');
	        $this->data['report_link'] = $this->url->link('phone/number/report' , '', 'SSL');
    	}
        $this->data['submitted_message'] = $this->language->get('submitted_message');
        $this->session->data['captcha']= '';
        
        $this->children = array(
            'common/column_left',
            'common/column_right',
            'common/content_top',
            'common/content_bottom',
            'common/footer',
            'common/header'
        );
        
        $this->template = 'ukphone/template/phone/process.tpl';

        $this->response->setOutput($this->render());
    }

    public function register() {
    	$this->language->load('phone/number');
    	
    	$this->document->setTitle($this->language->get('register_page_title'));
    	$this->document->setDescription($this->config->get('config_meta_description'));
    	$this->document->setKeywords($product_info['meta_keyword']);

    	$this->data['text_home'] = $this->language->get('text_home');
    	$this->data['text_report'] = $this->language->get('text_report');
    	
        if(isset($this->request->post)){
        	$this->load->model('phone/rate');
        	$this->load->model('phone/number');
        	$number = $this->model_phone_number->getNumber($this->request->post['phone_id']);
        	
        	$this->data['number'] = $number['number'];
        	if($this->model_phone_rate->setRate($this->request->post)){
        		$this->data['title'] = sprintf($this->language->get('title_vote_success'), $this->url->link('phone/number/search' , 'num=' . $number['number'], 'SSL'), $number['number']);
        		$this->data['success'] = $this->language->get('registered_message');
        	}
        	else{
        		$this->data['title'] = sprintf($this->language->get('title_vote_error'), $this->request->post['comment_id'], $this->url->link('phone/number/search' , 'num=' . $number['number'], 'SSL'), $number['number']);
        		$this->data['error'] = sprintf($this->language->get('already_registered_message'), $this->request->post['comment_id']);
        	}
        	
        	if(!empty($number)){
        		
        		$this->data['text_back'] = sprintf($this->language->get('text_back'), $number['number']);
        		$this->data['back_link'] = $this->url->link('phone/number/search' , 'num=' . $number['number'], 'SSL');
        	}
        }
        
       
        if(empty($number)){
        	$this->data['number'] = '';
        	$this->data['title'] = $this->language->get('title_vote');
        }
        
        if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
        	$this->data['home_link'] = $this->config->get('config_ssl');
        } else {
        	$this->data['home_link'] = $this->config->get('config_url');
        }
        
        $this->data['report_link'] = $this->url->link('phone/report' , '', 'SSL');
        
        $this->children = array(
        		'common/column_left',
        		'common/column_right',
        		'common/content_top',
        		'common/content_bottom',
        		'common/footer',
        		'common/header'
        );
        
        $this->template = 'ukphone/template/phone/register.tpl';
        
        $this->response->setOutput($this->render());
    }
    
    public function captcha() {
        $this->load->library('captcha');

        $captcha = new Captcha();

        $this->session->data['captcha'] = $captcha->getCode();

        $captcha->showImage();
    }
    
    protected function getValueInArray($data, $key)
    {
    	$value = '';
    	
    	foreach($data as $d){
    		if($d['call_type_id'] == $key){
    			$value = $d['name'];
    			break;
    		}
    	}
    	return $value;
    }

    protected function validate() {
    	$this->language->load('phone/number');
    	if (empty($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
    		$this->error['captcha'] = $this->language->get('error_captcha');
    	}
    	
    	if(!isset($this->request->post['number_id']))
    	{
	    	if (!isset($this->request->post['number']) || (isset($this->request->post['number']) && (strlen($this->request->post['number']) < 3 || strlen($this->request->post['number']) > 20))) {
	    		$this->error['number'] = $this->language->get('error_number');
	    	}
    	}
    	
    	if (!isset($this->request->post['comment']) || (isset($this->request->post['comment']) && (strlen($this->request->post['comment']) < 15 || strlen($this->request->post['comment']) > 4500))) {
    		$this->error['number'] = $this->language->get('error_comment');
    	}
 
    	if (!$this->error) {
    		return true;
    	} else {
    		return false;
    	}
    }
}
?>
